# Python modules
import re
import os
import os.path

class Template( object ):
	"""Represents a .html template of a website project.

	"""
	def __init__( self, relpath_template ):
		"""Constructor

		Params
			relpath_template
			string
			The relative path to a template.

		"""
		# Name of a template.
		self.name = os.path.basename( relpath_template )
		# Content of a template.
		with open( relpath_template, "r" ) as f:
			self.content = f.read()
			
		self.containers = self.extract_containers()
		self.main_navigation_containers, self.sub_navigation_containers = self.extract_navigation_containers()


	def extract_containers( self ):
		"""Returns a tuple with the containers that have been extracted of a
		template.

		"""
		containers = []
		result = re.findall( "#[A-Za-z0-9_]*#", self.content )
		if result is not None:
			for r in result:
				containers.append( r )

		return tuple( containers )


	def extract_navigation_containers( self ):
		"""Returns a tuple with the navigation containers that have been
		extracted of the template.

		"""
		main_navigation_containers = []
		sub_navigation_containers = []
		
		# Main navigation.
		result = re.findall( "%mainnavigation%", self.content )
		if result is not None:
			for r in result:
				main_navigation_containers.append( r )


		# Sub navigation.
		result = re.findall( "%subnavigation%", self.content )
		if result is not None:
			for r in result:
				sub_navigation_containers.append( r )

		return tuple( main_navigation_containers ), tuple( sub_navigation_containers )
